package cn.broadmax.boot.modules.aj.controller;


import cn.broadmax.boot.base.SuperController;
import cn.broadmax.boot.base.message.SResult;
import cn.broadmax.boot.modules.aj.bean.ajManageBackPageSearch;
import cn.broadmax.boot.modules.aj.bean.ajManageOutPageSearch;
import cn.broadmax.boot.modules.aj.bean.ajManagePageSearch;
import cn.broadmax.boot.modules.aj.entity.JzManage;
import cn.broadmax.boot.modules.aj.mapper.JzManageMapper;
import cn.broadmax.boot.modules.aj.service.IJzManageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jexx.util.Console;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 卷宗管理 前端控制器
 * </p>
 *
 * @author jexx
 * @since 2021-06-03
 */
@Api(tags = "卷宗管理(二级目录)")
@RestController
@RequestMapping("/aj/jz-manage")
@RequiredArgsConstructor
public class JzManageController extends SuperController {
    @Autowired
    private final IJzManageService jzManageService;

    @Autowired
    private JzManageMapper jzManageMapper;

    @ApiOperation("分页查询案卷类型")
    @GetMapping(value = "")
    public SResult<IPage<JzManage>> pageTypes(ajManagePageSearch search){
        LambdaQueryWrapper<JzManage> wrapper = ajManagePageSearch.createWrap(search);
        IPage<JzManage> page = jzManageService.page(getPage(), wrapper);
        return SResult.ok("", page);
    }

    @ApiOperation("分页查询出库记录")
    @GetMapping(value = "/out")
    public SResult<IPage<JzManage>> pageOfOut(ajManageOutPageSearch search){
        LambdaQueryWrapper<JzManage> wrapper = ajManageOutPageSearch.createWrap(search);
        IPage<JzManage> page = jzManageService.page(getPage(), wrapper);
//        Console.log("111");
        return SResult.ok("", page);
    }

    @ApiOperation("分页查询归还记录")
    @GetMapping(value = "/back")
    public SResult<IPage<JzManage>> pageOfBack(ajManageBackPageSearch search){
        LambdaQueryWrapper<JzManage> wrapper = ajManageBackPageSearch.createWrap(search);
        IPage<JzManage> page = jzManageService.page(getPage(), wrapper);
        Console.log("归还记录");
        return SResult.ok("", page);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    class Remind{
        String remind;
    }

    @ApiOperation("归还时间超时提醒")
    @GetMapping(value = "/remind")
    public SResult<List<Remind>> getRemind() throws ParseException {
        ArrayList<Remind> list = new ArrayList<>();

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("status","临时出库");
        List<JzManage> tempOutList = jzManageMapper.selectList(wrapper);
        for (JzManage temp : tempOutList) {
            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
            SimpleDateFormat simpleDateFormatNorm = new SimpleDateFormat("yyyy/MM/dd' 'HH:mm");
            Date date = simpleDateFormat.parse(temp.getTempPreBackTime().toString());
            long ts = date.getTime();
//            res = String.valueOf(ts);
            if (ts <= new Date().getTime()){
//                list.add(temp.getName() + " 案卷已经到期，请及时归还，案卷已经到期，请及时归还 " +simpleDateFormatNorm.format(ts));

                Remind remind = new Remind();
                remind.setRemind(temp.getName() + " 案卷已经到期，请及时归还，案卷已经到期，请及时归还 " +simpleDateFormatNorm.format(ts));
                list.add(remind);
            }
        }
        System.out.println(list);
        return SResult.ok("",list);
    }


    public Date compare(String time1,String time2) throws ParseException {
        //如果想比较日期则写成"yyyy-MM-dd"就可以了
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //将字符串形式的时间转化为Date类型的时间
        Date a=sdf.parse(time1);
        Date b=sdf.parse(time2);
        //Date类的一个方法，如果a早于b返回true，否则返回false
        if(a.before(b))
            return a;
        else
            return null;
		/*
		 * 如果你不喜欢用上面这个太流氓的方法，也可以根据将Date转换成毫秒
		if(a.getTime()-b.getTime()<0)
			return true;
		else
			return false;
		*/
    }

    @ApiOperation("添加")
    @PutMapping(value = "/insert")
    public SResult<Boolean> InsertPage(@RequestBody JzManage jzManage){
        jzManageMapper.insert(jzManage);
        return SResult.ok("", Boolean.TRUE);
    }

    @ApiOperation("案卷备注修改")
    @PutMapping(value = "/update/{id}")
    public SResult<Boolean> updatePage(@PathVariable Integer id, @RequestBody JzManage jzManage){
        jzManage.setId(id);
        jzManageService.updateById(jzManage);
        return SResult.ok("", Boolean.TRUE);
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/delete/{id}")
    public SResult<Boolean> DeletePage(@PathVariable String id){
        jzManageService.removeById(id);
        return SResult.ok("", Boolean.TRUE);
    }
}
